package arithmetic.baseAlgorithm.search;

/**
 * 二分查找法
 *
 * @author zhangyanqi
 * @since 1.0 2018/1/3
 */
public class BinarySearch {


    public static int binarySearch(int[] a, int fromIndex, int toIndex, int value) {
        int low = fromIndex;
        int high = toIndex - 1;
        while (low <= high) {
            int mid = low + ((high - low) / 2);
            int midVal = a[mid];
            if (midVal < value)
                low = mid + 1;
            else if (midVal > value)
                high = mid - 1;
            else return mid;
        }
        return -1;
    }
}
